//
//  ChooseMapViewController.m
//  EPBPro
//
//  Created by wucha on 2018/7/31.
//  Copyright © 2018年 fc. All rights reserved.
//

#import "ChooseMapViewController.h"

@interface ChooseMapViewController ()<MAMapViewDelegate>{
    CLLocationCoordinate2D coordinate;
    ChooseMapBlock chooseMapBlock;
}
@property (nonatomic,strong) MAMapView *mapView;

@end

@implementation ChooseMapViewController

-(id)initWithChooseBlock:(ChooseMapBlock)block{
    if (self == [super init]) {
        chooseMapBlock = block;
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    UIButton *rightbutton  = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 44)];
    [rightbutton setTitle:@"确定" forState:UIControlStateNormal];
    [rightbutton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [rightbutton addTarget:self action:@selector(submitAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightbutton];
    self.navigationItem.rightBarButtonItem = rightBarButtonItem;
    
    MAMapView *mapView = [[MAMapView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 400)];
    //    self.mapView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    mapView.delegate = self;
    //    [self.view addSubview:self.mapView];
    mapView.showsCompass = NO;
    [self.view addSubview:mapView];
    self.mapView = mapView;

    UIImageView *localImageView = [UIImageView new];
    localImageView.image = [UIImage imageNamed:@"nearby"];
    [self.view addSubview:localImageView];
    localImageView.sd_layout.centerXEqualToView(self.view).centerYIs(200-14).widthIs(28).heightIs(28);
    
    coordinate = [self.mapView convertPoint:CGPointMake(SCREEN_WIDTH/2, 200) toCoordinateFromView:self.view];

}

-(void)submitAction:(id)sender{
    if(chooseMapBlock){
        chooseMapBlock(coordinate);
    }
    [self.navigationController popViewControllerAnimated:YES];
}


#pragma mark - MAMapViewDelegate
- (void)mapView:(MAMapView *)mapView regionDidChangeAnimated:(BOOL)animated{
    coordinate = [self.mapView convertPoint:CGPointMake(SCREEN_WIDTH/2, 200) toCoordinateFromView:self.view];
    NSLog(@"---%f-------%f",coordinate.longitude,coordinate.latitude);
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
